#-*- coding:utf-8 -*-
import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
import os
import csv
class CreateProject(QWidget):

    new_project_Signal=Signal(str)
    def __init__(self,PROJECT_INFO_PATH,parent=None):
        super(CreateProject,self).__init__()
        self.setStyleSheet(
            'QWidget{background-color:rgb(75,75,75);color:rgb(200,200,200)}QPushButton{background-color:rgb(100,100,100);border:2px;border-radius:2px;padding:3px}QPushButton:hover{color:#f3995e}')
        self._parent=parent
        self.dir=os.path.dirname(os.path.dirname(__file__))
        self.PROJECT_INFO_PATH=PROJECT_INFO_PATH#os.path.join(self.dir,'projects')#外包项目不会很多

        self.setWindowTitle('创建项目')
        # self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.mainLayout=QVBoxLayout()


        self.projectName_layout=QHBoxLayout()
        self.projectName_layout.setAlignment(Qt.AlignCenter)
        self.projectName_layout.setContentsMargins(15,30,15,5)
        self.projectName_label=QLabel('项目名称:')
        # self.projectName_label.setFixedWidth(90)
        self.projectName_edit=QLineEdit()
        # self.projectName_edit.setFixedWidth(160)
        self.projectName_layout.addWidget(self.projectName_label)
        self.projectName_layout.addWidget(self.projectName_edit)


        self.projectType_layout=QHBoxLayout()
        # self.projectType_layout.setAlignment(Qt.AlignCenter)
        self.projectType_layout.setContentsMargins(15,20,15,5)
        self.projectType_label=QLabel('项目类型:')
        self.projectType_label.setFixedWidth(100)
        self.project_movie_radio_btn=QRadioButton('电影')
        self.project_tv_radio_btn = QRadioButton('电视剧')
        self.projectType_layout.addWidget(self.projectType_label)
        self.projectType_layout.addWidget(self.project_movie_radio_btn)
        self.projectType_layout.addWidget(self.project_tv_radio_btn)


        self.projectPath_layout=QHBoxLayout()
        # self.projectPath_layout.setAlignment(Qt.AlignCenter)
        self.projectPath_layout.setContentsMargins(15, 20, 15, 5)
        self.projectPath_label=QLabel('项目路径:')
        # self.projectPath_label.setFixedWidth(100)
        self.projectPath_edit=QLineEdit()
        # self.projectPath_edit.setFixedWidth(150)
        self.projectPath_btn=QPushButton('…')
        self.projectPath_btn.clicked.connect(self.getProjectPath)
        self.projectPath_btn.setFixedWidth(20)
        self.projectPath_layout.addWidget(self.projectPath_label)
        self.projectPath_layout.addWidget(self.projectPath_edit)
        self.projectPath_layout.addWidget(self.projectPath_btn)




        self.btn_layout=QHBoxLayout()
        # self.btn_layout.setAlignment(Qt.AlignCenter)
        self.btn_layout.setContentsMargins(25, 30, 25, 5)
        self.project_OK_btn=QPushButton('创建')
        self.project_OK_btn.clicked.connect(self.createProject)
        self.project_OK_btn.setFixedWidth(50)
        self.project_No_btn=QPushButton('取消')
        self.project_No_btn.clicked.connect(self.closeWindow)
        self.project_No_btn.setFixedWidth(50)
        self.btn_layout.addWidget(self.project_OK_btn)
        self.btn_layout.addWidget(self.project_No_btn)






        self.mainLayout.addLayout(self.projectName_layout)
        self.mainLayout.addLayout(self.projectType_layout)
        self.mainLayout.addLayout(self.projectPath_layout)
        self.mainLayout.addLayout(self.btn_layout)



        self.setLayout(self.mainLayout)



    def getProjectType(self):
        '''
        检测所选择的项目类型，当为0时，没有选择，当为1时表示电影，2是电视剧
        :return:
        '''
        movie=self.project_movie_radio_btn.isChecked()
        tv=self.project_tv_radio_btn.isChecked()
        if not movie and not tv:
            QMessageBox.information(self,'提示','请选择项目类型')
            return 0
        elif movie:
            return 1
        else:
            return 2
    def createProject(self):
        '''
        该函数可以创建项目，保存项目信息
        :return:注意创建完了之后 刷新主页面项目列表
        '''
        project_name=self.projectName_edit.text()
        project_type=self.getProjectType()
        dir_path=self.projectPath_edit.text()
        project_path=''
        if project_name and dir_path:
            project_path=os.path.join(dir_path,project_name)

            if not os.path.exists(project_path):

                self.writeProjectInfo(project_name,project_type,project_path)#写入本地projects数据库
                self.new_project_Signal.emit(project_name) #发射给主界面刷新项目列表
                os.makedirs(project_path)
                QMessageBox.information(self, 'OK', '创建成功')
                self.close()
            else:
                QMessageBox.information(self,'提示','该项目已经存在')

        else:
            QMessageBox.information(self,'提示','请输入完整项目信息')

    def getProjectPath(self):
        '''
        获取文件夹路径
        :return:
        '''
        dir=QFileDialog.getExistingDirectory(self,'选择文件夹')
        self.projectPath_edit.setText(dir)
        print(dir)
    def closeWindow(self):
        self.close()



    def writeProjectInfo(self,project_name,project_type,project_path):
        '''
        该函数实现将已经创建的项目信息写入到本地Project_Info当中。这样可以在主界面中一开始读取这些已有的项目
        :return:
        '''
        if not self.projectCheck(self.PROJECT_INFO_PATH,project_name):
            # project_info=[project_name,projectType,projectPath]

            project_info={}
            project_info_path=os.path.join(self.PROJECT_INFO_PATH,project_name)
            project_info['project_name']=project_name
            project_info['project_type']=project_type
            project_info['project_path']=project_path
            # with open(self.PROJECT_INFO_PATH,'a','utf-8') as f: #不需要用csv,项目不多，就一个项目一个文件干脆利落
            #     csv_writer=csv.writer(f,dialect='excel')

            with open(project_info_path,'w',encoding='utf-8') as w:
                w.write(str(project_info))
        else:
            QMessageBox.information(self,'注意','该项目已存在')


    def projectCheck(self,project_info_path,projectName):
        '''
        该函数用来检测要创建的项目是否已经存在
        :param project_info_path:
        :param projectName:
        :return:
        '''
        project_list=os.listdir(project_info_path)
        if projectName not in project_list:
            return False
        else:
            return True




if __name__ == '__main__':
    app=QApplication(sys.argv)
    cp=CreateProject()
    cp.show()
    sys.exit(app.exec_())

    # print(os.listdir('../projects'))

    # with open("F:/PycharmOut/BrickMover/projects/", 'w', encoding='utf-8') as w:
    #     w.write('没有权限')